from database.db_handler import MysqlHander
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib

'''
十大股东数据
'''

entity_gupiao_tag = "0"
entity_gudong_tag = "2"

file = open('./out/shidagudong.sql', 'w', encoding = 'utf-8')

class GudongRelation:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        self.current_stock = ""
        
    def insert_gudong_gupiao(self, gupiao, gudong, order_tag):
        md5_code = hashlib.md5(gudong.encode(encoding="UTF-8")).hexdigest()
        insert_gudong = "insert into entity_gudong (Fcode, Fname, Fcreate_time, Fmodify_time) values "
        insert_gudong += "('" + md5_code + "','" + gudong + "', now(), now());"
        
        #print(insert_gudong)
        try:
            #pass
            self.db.execute_not_safe(insert_gudong)
            file.write(insert_gudong + "\n")
        except Exception as e:
            print(str(e))
            pass
    
        #股东 - 股票
        sql1 = "insert into relation_data_01 (Fsrc_code, Fsrc_type, Frelation_id, Fdst_code, Fdst_type, Forder_tag, Fcreate_time, Fmodify_time) values "
        sql1 += "('" + md5_code + "'," + entity_gudong_tag + ",1004,'" + gupiao + "'," + entity_gupiao_tag + ", " + str(order_tag) + ", now(), now());"
        print(sql1)
        try:
            #pass
            self.db.execute_not_safe(sql1)
            #file.write(sql1 + "\n")
        except Exception as e:
            print(str(e))
            pass
            
        #股票 - 股东
        sql2 = "insert into relation_data_01 (Fsrc_code, Fsrc_type, Frelation_id, Fdst_code, Fdst_type, Forder_tag, Fcreate_time, Fmodify_time) values "
        sql2 += "('" + gupiao + "'," + entity_gupiao_tag + ",1003,'" + md5_code + "'," + entity_gudong_tag + ", " + str(order_tag) + ", now(), now());"
        print(sql2)
        try:
            #pass
            self.db.execute_not_safe(sql2)
            #file.write(sql2 + "\n")
        except Exception as e:
            print(str(e))
            pass
        #print(str(res))
    
    def delete_data(self):
        #股东 - 股票
        sql1 = "delete from relation_data_01 where Frelation_id in (1003,1004);"
        print(sql1)
        try:
            #pass
            self.db.execute_not_safe(sql1)
            #file.write(sql1 + "\n")
        except Exception as e:
            print(str(e))
            pass


    def read_data(self, file):
        f = open(file, encoding='utf-8')
        self.lines = f.readlines()
        f.close()
    
    def process_data(self):
        order_tag = 1
        for line in self.lines:
            ds = line.split("|")
            if len(ds) != 3:
                continue
            if self.current_stock != ds[0]:
                order_tag = 1
            self.insert_gudong_gupiao(ds[0], ds[1], order_tag)
            ++order_tag
    
    def get_data(self, file):
        self.read_data(file)
        self.process_data()


if __name__ == '__main__':
    d = GudongRelation()
    d.delete_data()
    d.get_data("./txt/shidagudong.txt")
    file.close()
